// pages/logistics/logistics.js
var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        content: "",
        is_supplypower: "",
        nowList: "",
        source_type: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            is_supplypower: options.is_supplypower,
            source_type: options.source_type
        });
        this.getData(options.trade_no, options.is_supplypower, options.source_type);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getTime(T) {
        var dt = new Date(T);
        var year = dt.getFullYear();
        var mon = dt.getMonth() + 1;
        var date = dt.getDate();
        var hour = dt.getHours();
        var minute = dt.getMinutes();
        var second = dt.getSeconds();
        mon = mon > 9 ? mon : "0" + mon;
        date = date > 9 ? date : "0" + date;
        hour = hour > 9 ? hour : "0" + hour;
        minute = minute > 9 ? minute : "0" + minute;
        second = second > 9 ? second : "0" + second;
        return year + "-" + mon + "-" + date + " " + hour + ":" + minute + ":" + second;
    },
    //获取数据
    getData: function(trade_no, is_supplypower, source_type) {
        let that = this;
        if (source_type == 7) {
            common.request({
                endpoint: "Wwb/index/express-query",
                method: "post",
                data: {
                    order_item_id: trade_no
                },
                success: res => {
                    that.setData({
                        nowList: res.extra
                    });
                }
            });
        } else {
            if (is_supplypower == -1) {
                common.request({
                    endpoint: "v1/Powersupply/manage/express-query",
                    method: "post",
                    data: {
                        order_item_id: trade_no
                    },
                    success: res => {
                        let arr = [];
                        res.extra.list.forEach((item, index) => {
                            item.time = that.getTime(item.time);
                            arr.push(item);
                        });
                        let cc = res.extra;
                        cc.list = arr;
                        that.setData({
                            content: cc
                        });
                    },
                    fail: function(res) {
                        wx.showModal({
                            title: "提示",
                            content: res.message,
                            success: function() {
                                wx.navigateBack();
                            }
                        });
                    }
                });
            } else {
                common.request({
                    endpoint: "Express/express/delivery_query",
                    method: "post",
                    data: {
                        trade_no: trade_no
                    },
                    success: res => {
                        that.setData({
                            content: res.extra
                        });
                    },
                    fail: function(res) {
                        wx.showModal({
                            title: "提示",
                            content: res.message,
                            success: function() {
                                wx.navigateBack();
                            }
                        });
                    }
                });
            }
        }
    }
});